package com.itheima.leetcode.od.a.greedy;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * <h3>高效的任务规划</h3>
 */
public class EfficientTaskPlanning {
    static class Machine {
        int b;
        int j;

        Machine(int b, int j) {
            this.b = b;
            this.j = j;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < m; i++) {
            int n = Integer.parseInt(sc.nextLine());
            List<Machine> list = new ArrayList<>();
            for (int j = 0; j < n; j++) {
                String[] parts = sc.nextLine().split(" ");
                list.add(new Machine(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])));
            }
            // 按运行时间J降序
            list.sort((a, b) -> b.j - a.j);

            int setupTime = 0;
            int maxTime = 0;
            for (Machine machine : list) {
                setupTime += machine.b;
                maxTime = Math.max(maxTime, setupTime + machine.j);
            }
            System.out.println(maxTime);
        }
    }
}